package com.siemens.industrial.web.mvc.controller.admin;

import com.siemens.industrial.model.JobType;
import com.siemens.industrial.model.admin.SystemConfig;
import com.siemens.industrial.model.admin.User;
import com.siemens.industrial.utils.*;
import com.siemens.industrial.web.mvc.pojo.DataTableQuery;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.WriteLogs;
import com.siemens.industrial.web.mvc.service.admin.DBBackupLogService;
import com.siemens.industrial.web.mvc.service.admin.SystemConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by David on 2016/11/17.
 */
@Controller
public class DBBackupController {
    @Resource
    DBBackupLogService dbBackupLogService;

    @Resource
    SystemConfigService systemConfigService;

    @Resource
    AuthService authService;

    @Resource
    WriteLogs writeLogs;

    @RequestMapping(value = "/admin/dbManager/page", method = RequestMethod.GET)
    public String getBackupPage() {
        return "admin/dbBackup";
    }

    @RequestMapping(value = "/admin/dbManager/setting", method = RequestMethod.GET)
    public String getBackupSettingPage() {
        return "admin/dbBackup_setting";
    }

    @RequestMapping(value = "/admin/dbManager/api/v1/getDBBackupLog", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDBBackupLog() {
        List<Map<String, Object>> list = dbBackupLogService.getDBBackupLog();
        return RestResponse.create(GlobalResponseCode.SUCCESS).put("backupInfo", list).build();
    }

    @RequestMapping(value = "/admin/api/v1/getDBBackupPlan", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDBBackupPlan() {
        SystemConfig systemConfig = systemConfigService.getOneByConfigKey(SystemConfig.DBBACKUP_KEY);
        Map<String, Object> map = null;
        if (systemConfig != null) {
            map = JsonToOtherFormat.buildJsonToMap(systemConfig.getSystemValue());
        }
        return RestResponse.create(GlobalResponseCode.SUCCESS).put("backupPlan", map).build();
    }

    @RequestMapping(value = "/admin/api/v1/backupNow", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> backupNow() {
        SocketClient socketClient = new SocketClient();
        if(!socketClient.sendRequest(JobType.DB_Backup_Execute.toString())){
            return RestResponse.create(GlobalResponseCode.ERROR_DBBACKUP_IMMEDIATELY_FAIL).build();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        User currentUser = (User) authService.currentUser();
        String content = currentUser.getUserName() + "于" + simpleDateFormat.format(new Date()) + "启用了实时备份。";
        writeLogs.writeLog(SystemConfig.LOG_USER_OPERATE_KEY, content, "database_operate");
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    //dataTable search backup log
    @RequestMapping(value = "/admin/dbManager/api/v1/getDBBackupLogByDataTable", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDBBackupLogByDataTable(HttpServletRequest request) {
        DataTableQuery dt = new DataTableQuery(request);
        if(dt.isDataTableRequest()){
            Map<String, Object> list = dbBackupLogService.getDBBackupLogByDataTable(request);
            return list;
        }

        return dt.buildResponse(0,0,new ArrayList<>());
    }
}
